﻿namespace System.Xml.Serialization.Internal
{
    using System.Globalization;
    using System.IO;

    internal sealed class ByteArraySerializer : XmlSerializer<byte[]>
    {
        private ByteArraySerializer() { }
        private static ByteArraySerializer _instance = new ByteArraySerializer();
        public static ByteArraySerializer Instance { get { return _instance; } }
        protected override byte[] Read(string value)
        {
            try
            {
                using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(value)))
                {
                    return ms.ToArray();
                }
            }
            catch
            {
                return null;
            }
        }
        protected override string Write(byte[] value)
        {
            try
            {
                using (MemoryStream ms = new MemoryStream(value))
                {
                    return Convert.ToBase64String(ms.ToArray());
                }
            }
            catch
            {
                return null;
            }
        }
    }
    internal sealed class GuidSerializer : XmlSerializer<Guid>
    {
        private GuidSerializer() { }
        private static GuidSerializer _instance = new GuidSerializer();
        public static GuidSerializer Instance { get { return _instance; } }
        protected override Guid Read(string value)
        {
            return new Guid(value);
        }
        protected override string Write(Guid value)
        {
            return value.ToString();
        }
    }
    internal sealed class EnumSerializer : XmlSerializer<object>
    {
        private EnumSerializer() { }
        private static EnumSerializer _instance = new EnumSerializer();
        public static EnumSerializer Instance { get { return _instance; } }
        protected override object Read(string value)
        {
            return Enum.Parse(RuntimeType, value, false);
        }
        protected override string Write(object value)
        {
            return value.ToString();
        }
    }
    internal sealed class StringSerializer : XmlSerializer<string>
    {
        private StringSerializer() { }
        private static StringSerializer _instance = new StringSerializer();
        public static StringSerializer Instance { get { return _instance; } }
        protected override string Read(string value)
        {
            return value;
        }
        protected override string Write(string value)
        {
            return value;
        }
    }
    internal sealed class Int16Serializer : XmlSerializer<Int16>
    {
        private Int16Serializer() { }
        private static Int16Serializer _instance = new Int16Serializer();
        public static Int16Serializer Instance { get { return _instance; } }
        protected override Int16 Read(string value)
        {
            return Int16.Parse(value, CultureInfo.InvariantCulture);
        }
        protected override string Write(Int16 value)
        {
            return value.ToString(CultureInfo.InvariantCulture);
        }
    }
    internal sealed class Int32Serializer : XmlSerializer<Int32>
    {
        private Int32Serializer() { }
        private static Int32Serializer _instance = new Int32Serializer();
        public static Int32Serializer Instance { get { return _instance; } }
        protected override Int32 Read(string value)
        {
            return Int32.Parse(value, CultureInfo.InvariantCulture);
        }
        protected override string Write(Int32 value)
        {
            return value.ToString(CultureInfo.InvariantCulture);
        }
    }
    internal sealed class Int64Serializer : XmlSerializer<Int64>
    {
        private Int64Serializer() { }
        private static Int64Serializer _instance = new Int64Serializer();
        public static Int64Serializer Instance { get { return _instance; } }
        protected override Int64 Read(string value)
        {
            return Int64.Parse(value, CultureInfo.InvariantCulture);
        }
        protected override string Write(Int64 value)
        {
            return value.ToString(CultureInfo.InvariantCulture);
        }
    }
    internal sealed class UInt16Serializer : XmlSerializer<UInt16>
    {
        private UInt16Serializer() { }
        private static UInt16Serializer _instance = new UInt16Serializer();
        public static UInt16Serializer Instance { get { return _instance; } }
        protected override UInt16 Read(string value)
        {
            return UInt16.Parse(value, CultureInfo.InvariantCulture);
        }
        protected override string Write(UInt16 value)
        {
            return value.ToString(CultureInfo.InvariantCulture);
        }
    }
    internal sealed class UInt32Serializer : XmlSerializer<UInt32>
    {
        private UInt32Serializer() { }
        private static UInt32Serializer _instance = new UInt32Serializer();
        public static UInt32Serializer Instance { get { return _instance; } }
        protected override UInt32 Read(string value)
        {
            return UInt32.Parse(value, CultureInfo.InvariantCulture);
        }
        protected override string Write(UInt32 value)
        {
            return value.ToString(CultureInfo.InvariantCulture);
        }
    }
    internal sealed class UInt64Serializer : XmlSerializer<ulong>
    {
        private UInt64Serializer() { }
        private static UInt64Serializer _instance = new UInt64Serializer();
        public static UInt64Serializer Instance { get { return _instance; } }
        protected override ulong Read(string value)
        {
            return ulong.Parse(value, CultureInfo.InvariantCulture);
        }
        protected override string Write(ulong value)
        {
            return value.ToString(CultureInfo.InvariantCulture);
        }
    }
    internal sealed class DecimalSerializer : XmlSerializer<Decimal>
    {
        private DecimalSerializer() { }
        private static DecimalSerializer _instance = new DecimalSerializer();
        public static DecimalSerializer Instance { get { return _instance; } }
        protected override Decimal Read(string value)
        {
            return Decimal.Parse(value, CultureInfo.InvariantCulture);
        }
        protected override string Write(Decimal value)
        {
            return value.ToString(CultureInfo.InvariantCulture);
        }
    }
    internal sealed class DoubleSerializer : XmlSerializer<double>
    {
        private DoubleSerializer() { }
        private static DoubleSerializer _instance = new DoubleSerializer();
        public static DoubleSerializer Instance { get { return _instance; } }
        protected override double Read(string value)
        {
            return double.Parse(value, CultureInfo.InvariantCulture);
        }
        protected override string Write(double value)
        {
            return value.ToString(CultureInfo.InvariantCulture);
        }
    }
    internal sealed class FloatSerializer : XmlSerializer<float>
    {
        private FloatSerializer() { }
        private static FloatSerializer _instance = new FloatSerializer();
        public static FloatSerializer Instance { get { return _instance; } }
        protected override float Read(string value)
        {
            return float.Parse(value, CultureInfo.InvariantCulture);
        }
        protected override string Write(float value)
        {
            return value.ToString(CultureInfo.InvariantCulture);
        }
    }
    internal sealed class ByteSerializer : XmlSerializer<byte>
    {
        private ByteSerializer() { }
        private static ByteSerializer _instance = new ByteSerializer();
        public static ByteSerializer Instance { get { return _instance; } }
        protected override byte Read(string value)
        {
            return byte.Parse(value, CultureInfo.InvariantCulture);
        }
        protected override string Write(byte value)
        {
            return value.ToString(CultureInfo.InvariantCulture);
        }
    }
    internal sealed class SByteSerializer : XmlSerializer<SByte>
    {
        private SByteSerializer() { }
        private static SByteSerializer _instance = new SByteSerializer();
        public static SByteSerializer Instance { get { return _instance; } }
        protected override SByte Read(string value)
        {
            return SByte.Parse(value, CultureInfo.InvariantCulture);
        }
        protected override string Write(SByte value)
        {
            return value.ToString(CultureInfo.InvariantCulture);
        }
    }
    internal sealed class BooleanSerializer : XmlSerializer<bool>
    {
        private BooleanSerializer() { }
        private static BooleanSerializer _instance = new BooleanSerializer();
        public static BooleanSerializer Instance { get { return _instance; } }
        protected override bool Read(string value)
        {
            return bool.Parse(value);
        }
        protected override string Write(bool value)
        {
            return value.ToString(CultureInfo.InvariantCulture);
        }
    }
}
